﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataMonitorWebApi.Quartz
{
    public  class QuartzService
    {
        private readonly ISchedulerFactory _schedulerFactory;

        public QuartzService(ISchedulerFactory schedulerFactory)
        {
            _schedulerFactory = schedulerFactory;
        }

        public async Task Start()
        {
            var scheduler = await _schedulerFactory.GetScheduler();
            await scheduler.Start();
        }

        public async Task Shutdown()
        {
            var scheduler = await _schedulerFactory.GetScheduler();
            await scheduler.Shutdown();
        }

        /// <summary>
        /// 添加监控任务，这里有2个，一个为监控数据，一个为监控日志
        /// </summary>
        /// <param name="jobName">这里的是数据的input路径</param>
        /// <param name="cron1"></param>
        /// <param name="cron2"></param>
        /// <returns></returns>
        public async Task AddJob(string jobName, string cron1, string cron2)
        {
            var scheduler = await _schedulerFactory.GetScheduler();
            //获得监控数据路径和监控日志路径
            var pathlist = jobName.Split(';');
            var jobKey1 = new JobKey(pathlist[0]);
            var job1 = JobBuilder.Create<MyJob>()
                        .WithIdentity(jobKey1)
                        .UsingJobData("datapath", pathlist[0])
                        .Build();
            var trigger1 = TriggerBuilder.Create()
                        .ForJob(jobKey1)
                        .WithIdentity(pathlist[0])
                        .WithCronSchedule(cron1)
                        .Build();
            await scheduler.ScheduleJob(job1, trigger1);

            var jobKey2 = new JobKey(pathlist[1]);
            var job2 = JobBuilder.Create<MyJobforLog>()
                        .WithIdentity(jobKey2)
                        .UsingJobData("logpath", pathlist[1])
                        .Build();
            var trigger2 = TriggerBuilder.Create()
                        .ForJob(jobKey2)
                        .WithIdentity(pathlist[1])
                        .WithCronSchedule(cron2)
                        .Build();
            await scheduler.ScheduleJob(job2, trigger2);
            Console.WriteLine($"Add job {pathlist[0]}");
            Console.WriteLine($"Add job {pathlist[1]}");
        }

        public async Task RemoveJob(string jobName)
        {
            var scheduler = await _schedulerFactory.GetScheduler();
            if (scheduler == null) throw new ArgumentNullException(nameof(scheduler));

            var jobKey = new JobKey(jobName);
            await scheduler.DeleteJob(jobKey);
            Console.WriteLine($"Delete job {jobName}");
        }

        public async Task PauseJob(string jobName)
        {
            var scheduler = await _schedulerFactory.GetScheduler();

            var jobKey = new JobKey(jobName);

            await scheduler.PauseJob(jobKey);
            Console.WriteLine($"Pause job {jobName}");
        }

        public async Task ResumeJob(string jobName)
        {
            var scheduler = await _schedulerFactory.GetScheduler();

            var jobKey = new JobKey(jobName);

            await scheduler.ResumeJob(jobKey);
            Console.WriteLine($"Resume job {jobName}");
        }
    }
}
